// LedSegment.cpp: implementation of the CLedSegment class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "8085.h"
#include "LedSegment.h"

#include <math.h>

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CLedSegment::CLedSegment()
{
    Init();
}

CLedSegment::~CLedSegment()
{

}

void CLedSegment::Init()
{           
    m_nPoints = sizeof(m_Points)/sizeof(m_Points[0]);
    for(int i=0;i<m_nPoints; i++)
        m_Points[i]=(0,0);
}

int CLedSegment::Create(int Xpos, int Ypos, int nLen, int nWidth, int nAngle)
{
    // create the various points which define a LED
    // all points are relative to the starting position, hence the starting 
    // position should be added to the starting point
    int i=0;

    m_Points[i].x = Xpos;
    m_Points[i].y = Ypos;
    i++;

    // First the leading edge
    //  /
    m_Points[i].x = m_Points[i-1].x + int((nWidth/2)*cos((nAngle+45)*(PI/180.0)));
    m_Points[i].y = m_Points[i-1].y - int((nWidth/2)*sin((nAngle+45)*(PI/180.0)));
    i++;

    // the straight line after the leading edge
    //    ________
    //  /
    m_Points[i].x = m_Points[i-1].x + int((nLen)*cos((nAngle)*(PI/180.0)));
    m_Points[i].y = m_Points[i-1].y - int((nLen)*sin((nAngle)*(PI/180.0)));
    i++;

    // the trailing edge
    //    ________
    //  /          \ 
    m_Points[i].x = m_Points[i-1].x +  int((nWidth/2)*cos((nAngle-45)*(PI/180.0)));
    m_Points[i].y = m_Points[i-1].y -  int((nWidth/2)*sin((nAngle-45)*(PI/180.0)));
    i++;
             
    // the trailing edge
    //    ________
    //  /          \ 
    //             /
    m_Points[i].x = m_Points[i-1].x - int((nWidth/2)*cos((nAngle+45)*(PI/180.0)));
    m_Points[i].y = m_Points[i-1].y + int((nWidth/2)*sin((nAngle+45)*(PI/180.0)));
    i++;

    // the straight line after the leading edge
    //    ________
    //  /          \
    //    ________ /
    m_Points[i].x = m_Points[i-1].x - int((nLen)*cos((nAngle)*(PI/180.0)));
    m_Points[i].y = m_Points[i-1].y + int((nLen)*sin((nAngle)*(PI/180.0)));
    i++;

    m_Points[i] = m_Points[0];
    return GetLength();
}

// Get the length of the LED segment 
int CLedSegment::GetLength()
{
    long xSqr;
    long ySqr;

    xSqr = (m_Points[0].x-m_Points[3].x)*(m_Points[0].x-m_Points[3].x);
    ySqr = (m_Points[0].y-m_Points[3].y)*(m_Points[0].y-m_Points[3].y);
    return (int)sqrt((double)(xSqr+ySqr));
}


void CLedSegment::Draw(CDC *pDC, COLORREF color)
{
    CBrush fill(color);
    CPen pen(PS_SOLID,1,color);
    // install the brush and pen and get the previous brush and pen
    // to be restored later 
    CPen *savePen = pDC->SelectObject(&pen);
    CBrush *saveBrush = pDC->SelectObject(&fill);

    pDC->Polygon(m_Points,6);
    // restore the saved pen and brush
    pDC->SelectObject(savePen);
    pDC->SelectObject(saveBrush);    // restore the saved brush
}

CPoint CLedSegment::GetFourthPoint()
{
    return m_Points[3];
}

int CLedSegment::Create(CPoint p, int nLen, int nWidth, int nAngle)
{
    return Create(p.x,p.y,nLen,nWidth,nAngle);
}

// get any of the points out of the 7 points which make a LED segment
CPoint CLedSegment::GetPoint(int nPos)
{
    ASSERT(nPos>=0 && nPos<m_nPoints);
    return m_Points[nPos];
}
